<?php
/**
 * This is the table class.	 This does NOT represent a table, but rather an object loaded from a table.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-10-30
 */

require_once 'database.php';

/**
 * This class is a Template for all of the table
 * classes. This way, some of the redundant functions
 * can be used without having to redefine them.
 */
class table {
	protected $id;
	protected $tab;

	/**
	 * The constructor of the table class.
	 *
	 * @param i	 The ID for this table object.
	 * @param t	 The table name for this object.
	 * @return	 The newly constructed object.
	 */
	function __construct ($i,$t) {
		$this->id = $i;
		$this->tab = $t;
	}

	/**
	 * Initialize an object from the database.
	 *
	 * @param db  The database to read from.
	 * @return	  None.
	 */
	function init ($db = NULL) {
		if (!$db)
			$db = new database ();

		$vars = $db->select_row_by_id ($this->tab, $this->id);

		if (!$vars)
			return FALSE;

		foreach ($vars as $key => $value) {
			$this->{$key} = $value;
		}
		return TRUE;
	}

	/**
	 * Get the ID of this table object.
	 */
	function id () {
		return $this->id;
	}

	/**
	 * Get the table name of this table object.
	 */
	function tab () {
		return $this->tab;
	}

	/**
	 * Insert the information of this table object to the database.
	 *
	 * @param $db  The database object with a connection to the database.
	 * @return	   Something.
	 */
	function insert_to_database($db) {
		$assoc = get_object_vars ($this);
		unset ($assoc['tab']); unset ($assoc['id']);
		$result = $db->insert_entry ($this->tab, $assoc);
		if ($result)
			$this->id = $db->insert_id ();
		return $result;
	}

	/**
	 * Update the information of this table object in the database.
	 *
	 * @param $db  The database object with a connection to the database.
	 * @return	   Something.
	 */
	function update_database_by_id ($db) {
		$assoc = get_object_vars ($this);
		unset ($assoc['tab']); unset ($assoc['id']);
		foreach ($assoc as $name => &$value) {
			$value = "'$value'";
		}
		$result = $db->update_entry_by_id ($this->tab, $assoc, $this->id);
		return $result;
	}

	/**
	 * Display the input form corresponding to this table object.
	 *
	 * @param table_name  The name of the table.
	 * @return			  None.
	 */
	static function display_form ($table_name) {
		echo "Add a new " . strtolower($table_name) . ": <br>\n";
		$type = strtolower ($table_name);
		$type = trim ($type, "s");
		$assoc = get_class_vars (get_called_class ());
		echo "<form name=\"$table_name\" action=\"insert.php?type=$type\" method=\"post\">\n";
		echo "<table> \n";
		foreach ($assoc as $key => $value) {
			if (!strcmp ($key, 'id') || !strcmp ($key, 'tab')) {
				continue;
			}
			echo "<tr>\n";
			$mod_key = ucwords(str_replace("_", " ", $key));
			echo "<td>" . $mod_key . "</td>\n";
			echo '<td> <input type="text" name="' . $key . '">' . "\n";
			echo "</tr>\n";
		}
		echo "</table><br>\n";
		echo '<input type="submit" value="Enter"> <br>'."\n";
		echo '</form>'."\n";
	}
}

?>